package pms.dao;

import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import pms.dto.ResEventKundeDTO;
import pms.om.Event;
import pms.om.Kunde;
import pms.om.Rechnung;
import pms.om.Reservierung;


/**
 * The Data access class for reservations. All Interaction with the database
 * regarding the entity bean Reservation should be handled by this class!
 * @author KRS DEV Team (krsdevteam@PMSWIbb2011SS.de)
 */
public class ReservierungDao extends HibernateDaoSupport {
	private ReservierungDao reservierungDao;
	
	public ReservierungDao getReservierungDao() {
		return reservierungDao;
	}

	public void setReservierungDao(ReservierungDao reservierungDao) {
		this.reservierungDao = reservierungDao;
	}
	
	public Reservierung findById(int id) {
		HibernateTemplate template = getHibernateTemplate();
		return (Reservierung) template.get(Reservierung.class, id);
	}

	/**
	 * creates a new persistent reservation in the database providing all the necessary details.
	 * @param i1 number of seats loge
	 * @param i2 number of seats regular
	 * @param d date of reservation
	 * @param e reference to event
	 * @param k reference to customer
	 * @param r reference to billing no. (not finished)
	 * @see Boot
	 */
	public void create(int i1, int i2, String d, Event e, Kunde k, Rechnung r) {
		HibernateTemplate template = getHibernateTemplate();
		Reservierung reservierung = new Reservierung(i1, i2, d, e, k, r);
		template.persist(reservierung);
	}
	
	/**
	 * deletes a reservierung using it's id.
	 * @param id reference to the reservation which should be deleted
	 */
	public void deleteById(int id) {
		HibernateTemplate template = getHibernateTemplate();
		Reservierung r = findById(id);
		template.delete(r);
		template.flush();
	}
	
	/**
	 * Returns all reservations from the database.
	 * @return a list of Reservations
	 * @see Reservation
	 */
	@SuppressWarnings("unchecked")
	public List<Reservierung> findAll() {
		HibernateTemplate template = getHibernateTemplate();
		return template.loadAll(Reservierung.class);
	}
		
	/**
	 * @return A sorted set of ResEventKundeDTO objects
	 * @see http://static.springframework.org/spring/docs/2.5.x/reference/orm.html#orm-hibernate-template
	 */
	@SuppressWarnings("unchecked")
	public List<ResEventKundeDTO> findAllVoll() {
		HibernateTemplate template = getHibernateTemplate();
		return (List<ResEventKundeDTO>) template.execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Criteria criteria = session.createCriteria(Reservierung.class);
				List<Reservierung> alleReservierungen = criteria.list();
				List<ResEventKundeDTO> liste = new LinkedList<ResEventKundeDTO>();
				for (Reservierung r : alleReservierungen) {
					ResEventKundeDTO rekdto = new ResEventKundeDTO(r);
					liste.add(rekdto);
				}	
				
				return liste;
			}
		});
	}
}
